home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / imagecompositing / src / controlpanel.java next >
Encoding:
Java Source  |  2000-09-28  |  6.3 KB  |  230 lines

  1. /*
  2.  * QuickTime for Java SDK Sample Code
  3.  
  4.    Usage subject to restrictions in SDK License Agreement
  5.  * Copyright: © 1996-1999 Apple Computer, Inc.
  6.  
  7.  */
  8. import java.awt.*;
  9. import java.awt.event.*;
  10.  
  11. import quicktime.*;
  12. import quicktime.app.*;
  13. import quicktime.app.display.*;
  14. import quicktime.app.anim.*;
  15. import quicktime.io.*;
  16. import quicktime.std.movies.*;
  17. import quicktime.std.*;
  18. import quicktime.std.image.*;
  19. /**
  20.  * A simple panel of AWT objects to demonstrate how AWT can be used to control a
  21.  * QuickTime movie.
  22.  */
  23. public class ControlPanel extends Panel implements Errors, StdQTConstants {
  24. //_________________________ CLASS METHODS
  25.     public ControlPanel (Compositor c, JavaTextDrawer t) throws QTException 
  26.     {
  27.         this.comp = c;
  28.         this.jText = t;
  29. //        this.layerUpdater = updater;
  30.         
  31.         GridBagLayout gb = new GridBagLayout();
  32.         setLayout (gb);
  33.         setFont (new Font("Helvetica", Font.BOLD, 10));
  34.  
  35.         GridBagConstraints cons = new GridBagConstraints();
  36.         cons.fill = GridBagConstraints.HORIZONTAL;
  37.         cons.gridheight = 1;        
  38.         cons.insets = new Insets (2, 2, 2, 2);        
  39.         cons.gridwidth = 2;
  40.         cons.gridy = 0;
  41.         cons.gridx = 0;
  42.         add (rateLabel, cons);
  43.  
  44.         cons.gridx = 2;
  45.         add (rateField, cons);
  46.         
  47.         cons.gridx = 4;
  48.         add (scaleLabel, cons);
  49.         
  50.         cons.gridx = 6;
  51.         add (scaleField, cons);
  52.         
  53.         cons.gridy = 1;
  54.         cons.gridx = 0;    
  55.         add (preflightCheck, cons);
  56.  
  57.         cons.gridx = 2;    
  58.         add (recordMovieButton, cons);
  59.  
  60.         cons.gridx = 4;
  61.         add (recordLabel, cons);
  62.         
  63.         cons.gridx = 6;
  64.         add (recordField, cons);
  65.  
  66.         cons.gridy = 2;
  67.         cons.gridx = 3;    
  68.         add (changeColorButton, cons);
  69.     
  70.         //set recordmovie
  71.         recMovie = new MovieRecording();
  72.         recMovie.setCodec();
  73.  
  74.         changeColorButton.addActionListener (new ActionListener () {
  75.                 public void actionPerformed (ActionEvent event) {
  76.                     jText.doWork();
  77.                 }
  78.             });
  79.  
  80.  
  81.         rateField.addActionListener (new ActionListener () {
  82.                 public void actionPerformed (ActionEvent event) {
  83.                     String str = new String (event.getActionCommand());
  84.                     comp.getTimer().setRate (new Float (str).floatValue());
  85.                 }
  86.             });
  87.         scaleField.addActionListener (new ActionListener () {
  88.                 public void actionPerformed (ActionEvent event) {
  89.                     String str = new String (event.getActionCommand());
  90.                     try {
  91.                         comp.getTimer().rescheduleTickle(new Integer (str).intValue(), 1);
  92.                     } catch (QTException e) {
  93.                         e.printStackTrace();
  94.                     }
  95.                 }
  96.             });    
  97.         recordField.addActionListener (new ActionListener () {
  98.                 public void actionPerformed (ActionEvent event) {
  99.                     String str = new String (event.getActionCommand());
  100.                     numRecordFrames = new Integer (str).intValue();
  101.                 }
  102.             });    
  103.             
  104.         recordMovieButton.addActionListener (new ActionListener () {
  105.             public void actionPerformed (ActionEvent event) {
  106.                 try {
  107.                     FileDialog fd = new FileDialog (ImageCompositing.pm, "Save Movie As...", FileDialog.SAVE);
  108.                     fd.show();
  109.                     if(fd.getFile() == null)
  110.                         throw new QTIOException (userCanceledErr, "");
  111.                     QTFile f = new QTFile(fd.getDirectory() + fd.getFile());
  112.                     Movie theMovie = Movie.createMovieFile (f,
  113.                                         kMoviePlayer, 
  114.                                         createMovieFileDeleteCurFile | createMovieFileDontCreateResFile);
  115.                     
  116.                     preflightCheck.setState(false); // recording disables preflight
  117.                 
  118.                     //Setup the record movie class
  119.                     recMovie.setMovie(theMovie, new CleanupMovie (f));
  120.                     recMovie.recordMode (numRecordFrames);
  121.                     comp.setRecordMovie (recMovie);
  122.                     
  123.                     System.out.println ("Start Recording");
  124.                     
  125.                 } catch (QTException e) {
  126.                     if (e.errorCode() != userCanceledErr)
  127.                         e.printStackTrace();
  128.                 }
  129.             }
  130.         });
  131.         
  132.         
  133.         //for preflighting
  134.         preflightCheck.addItemListener (new ItemListener () {
  135.                 public void itemStateChanged (ItemEvent ev) {
  136.                     try {
  137.                         if (((Checkbox)ev.getItemSelectable()).getState()) {
  138.                             recMovie.setPreflighting(true);
  139.                             comp.setRecordMovie (recMovie);
  140.                         }                
  141.                         else {
  142.                             recMovie.setPreflighting(false);
  143.                         }    
  144.                     } catch (QTException e) {
  145.                         e.printStackTrace();
  146.                     }
  147.                 }
  148.             });    
  149.             
  150.     }
  151.  
  152.         
  153.     private class CleanupMovie implements RecordMovieCallback {
  154.         CleanupMovie (QTFile f) {
  155.             this.f = f;
  156.         }
  157.         
  158.         private QTFile f;
  159.         
  160.         public void finish (Movie m) {
  161.             try {
  162.                 OpenMovieFile outStream = OpenMovieFile.asWrite (f); 
  163.                 m.addResource (outStream, movieInDataForkResID, f.getName());
  164.                 outStream.close();
  165.             } catch (QTException e) {
  166.                 e.printStackTrace();
  167.             }
  168.             System.out.println ("Finished Recording");
  169.         }
  170.     }
  171.     
  172.     private class MovieRecording extends RecordMovie {
  173.         // we record at 2 frames a second
  174.         int framesPerSecond = 10;
  175.  
  176.         MovieRecording() throws QTException {
  177.             super();
  178.         }
  179.         
  180.         public void setCodec() throws QTException{
  181.             setCompressionSettings (framesPerSecond, 
  182.                     codecNormalQuality, 
  183.                     codecNormalQuality, 
  184.                     0, 
  185.                     kAnimationCodecType, 
  186.                     CodecComponent.bestSpeedCodec);    
  187.                     
  188.         }            
  189.             
  190.     }
  191.     
  192.     
  193.     public void setDisplay () throws QTException {
  194.         rateField.setText (Float.toString (comp.getTimer().getRate()));
  195.         scaleField.setText (Integer.toString (comp.getTimer().getScale()));
  196.         recordField.setText (Integer.toString (numRecordFrames));
  197.     }
  198.     
  199. //_________________________ INSTANCE VARIABLES
  200.     private Label rateLabel = new Label ("Playback Rate:", Label.RIGHT);
  201.     private TextField rateField = new TextField (8);
  202.  
  203.     private Button changeColorButton = new Button ("Text Colour");
  204.  
  205.     private Label scaleLabel = new Label ("Scale (fps):", Label.RIGHT);
  206.     private TextField scaleField = new TextField (8);
  207.         
  208.     private Compositor comp;
  209.     private JavaTextDrawer jText;
  210.     private Button recordMovieButton = new Button("Record Movie");
  211.     private Label recordLabel = new Label ("Record Frames:", Label.RIGHT);
  212.     private TextField recordField = new TextField (8);
  213.     private Checkbox preflightCheck = new Checkbox ("Preflight", false);
  214.     
  215.     private boolean selected = false;
  216.     private MovieRecording recMovie;
  217.     private int numRecordFrames = 10;
  218.     int keyFrameRate = 0;
  219. //_________________________ INSTANCE METHODS
  220.     /**
  221.      * @return a Dimension object which defines the minimum size
  222.      */     
  223.     public Dimension getMinimumSize() { return new Dimension (0, 100); }
  224.  
  225.     /**
  226.      * @return a Dimension object which defines the preferred size
  227.      */     
  228.     public Dimension getPreferredSize() { return getMinimumSize(); }
  229. }
  230.